#include <vector>

using std::vector;

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int start = 0;
        int need_gas = 0;
        int total = 0;
        int i = 0;
        int n = gas.size();
        while (i < n) {
            total += (gas[i] - cost[i]);
            if (total < 0) {
                need_gas += total;
                start = i + 1;
                total = 0;
            }
            i++;
        }
        if (total + need_gas >= 0) {
            return start;
        }
        else {
            return -1;
        }
    }
};